
import { Component, OnInit, ViewChild } from '@angular/core';
import 'rxjs/add/operator/map';
import { CommService } from '../../comm/comm.service';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
declare const $: any;
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';
import { Observable } from 'rxjs/Rx';
import { ChartOptionCreater } from '../../model/comm/chart-option-creater';
// declare var table2excel: any;
@Component({
  selector: 'app-w-ftp',
  templateUrl: './w-ftp.component.html',
  styleUrls: ['./w-ftp.component.scss']
})
export class WFtpComponent implements OnInit {
  title = 'FTA指标';
  /*生产线 - 班次*/
  public subAreaItems: SubArea[];
  public subAreaSelected: any;
  public shiftItems: any[];
  public shiftSelected: any;
  public equipemntItems: TVResource[] = [];
  public equipmentSelected: TVResource[];
  public queryStationCode;
  public ftaChartOption: any;
  public ftaChartOptionQ: any;
  public moeeList = [];
  public moee = 0;
  public moeeJson;

  @ViewChild('dddb') dddb: DxDropDownBoxComponent;
  @ViewChild('ddda') ddda: DxDropDownBoxComponent;

  constructor(private commServices: CommService) {
  }

  ngOnInit() {
    this.commServices.initDatePicker($('#DateTimeRange'), 'YYYY-MM-DD');
    this.shiftItems = this.commServices.shifts;
    this.shiftSelected = this.shiftItems[0];
    this.subAreaItems = this.commServices.getSubAreas();
    this.subAreaSelected = [];
    // this.equipemntItems = [];
    // this.subAreaSelected.forEach(e => {
    //   const arr = this.commServices.getTVResourceBySubArea(e.subArea);
    //   arr.forEach(a => { this.equipemntItems.push(a); });
    // });

    // this.equipmentSelected = [];
    this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaItems[0].subArea);
    this.equipemntItems = this.equipemntItems.filter(item => item.ftpAnalyse === 1);
    this.equipmentSelected = this.equipemntItems;

  }

  clearEquipmentSelected() {
    this.equipmentSelected = [];
  }
  equipmentSelectedOk() {
    this.dddb.instance.close();
  }
  clearSubAreaSelected() {
    this.subAreaSelected = [];
  }
  subAreaSelectedOk() {
    this.ddda.instance.close();
  }

  onSubAreaSelectedChange(subArea: any): void {
    // this.equipemntItems = [];
    this.equipemntItems = this.commServices.getTVResourceBySubArea(subArea.value.subArea);
    this.equipemntItems = this.equipemntItems.filter(item => item.ftpAnalyse === 1);
    this.equipmentSelected = this.equipemntItems;
    // const arr = this.commServices.getTVResourceBySubArea(this.subAreaSelected.subArea);
    // arr.forEach(a => { this.equipemntItems.push(a); });
    const queryModel: QueryModel = new QueryModel();
    queryModel.querySubAreaIds = [this.subAreaSelected.subArea];
  }


  getReportData(): void {
    const queryModel: QueryModel = new QueryModel();
    queryModel.dateTimeRange = this.getTime('DateTimeRange');
    queryModel.querySubAreaIds = [this.subAreaSelected.subArea];
    queryModel.shift = this.shiftSelected.shiftId;
    queryModel.queryResourceIds = this.equipmentSelected.map(e => e.resource);
    this.commServices
      .getList(queryModel, '/prodCount/ftp')
      .subscribe(model => {
        if (model.resultCode === 200) {
          model.data.fta.forEach(e => e.ftp === -1 ? e.ftp = 'N/A' : e.ftp)
          this.moeeList = model.data.fta;
          this.moeeJson = model.data.json;
          this.showFTAChart();
          this.showFTAChartQ();
          debugger
        } else {
          alert('查询错误：' + model.resultMessage);
        }
      });
  }
  /**ECharts图表相关*/
  chartsMap = new Map<number, any>();
  onChartInit(index: number, echartsIntance: any): void {
    this.chartsMap.set(index, echartsIntance);
    Observable.fromEvent(window, 'resize')
      .debounceTime(100) // 以免频繁处理
      .subscribe(() => this.chartsMap.forEach((v) => v.resize()));
  }
  private showFTAChart(): void {
    const xData = new Array();
    const list1 = new Array();
    const series = new Array();
    this.moeeJson.forEach(u => {
      const station = u.station;
      xData.push(station);
      list1.push(u.avg);
    });
    series.push(
      {
        name: '平均值',
        type: 'line',
        data: list1
      }
    );
    const legend = ['平均值'];
    this.ftaChartOption = ChartOptionCreater
      .createChartsOptionFta('', '', '', '', legend, xData, series, null); // 
    // this.showloading = false;
  }
  private showFTAChartQ(): void {
    const xtime = new Array();
    const series = new Array();
    const opMap = new Map();
    const legend = new Array();
    this.moeeJson.sort();
    this.moeeList.forEach(u => {
      const station = u.prodDateVal0 + ' ' + u.shiftVal0;
      if (xtime.indexOf(station) == -1) {
        xtime.push(station);
      }
      //xtime.sort();
      const op = u.op;
      if (!opMap.has(op)) {
        opMap.set(op, []);
      }
      const arr = opMap.get(op);
      arr.push(u.ftp);
    });
    opMap.forEach((k, v) => {
      legend.push(v);

      series.push(
        {
          name: v,
          //color:'#1E90FF',
          type: 'bar',
          data: k,
        //   itemStyle: {
        //     normal: {
        //         color: '#63B8FF'
        //     },
        // },
        }
      );
    });
    debugger

    this.ftaChartOptionQ = ChartOptionCreater
      .createChartsOptionFta('', '', '', '', legend, xtime, series, null); // 
    // this.showloading = false;
  }
  selectedIndexChange(tabIndex: any) {
    this.chartsMap.get(tabIndex).resize();
  }

  getTime(id: any): String {
    return $('#' + id).val();
  }

  exportTable() {
    // tab2Excel.method1('fta', 'FTA指标' + new Date());
    $('#fta').table2excel({
      exclude: '.excludeThisClass',
      name: 'FTA',
      filename: 'FTA指标' + new Date().toISOString().replace(/[\-\:\.]/g, ''),
      fileext: '.xls',
      exclude_img: true,
      exclude_links: true,
      exclude_inputs: true
    });
  }

}